<?php
/**
 * 测试
 *
 * @author 孙晓晔
 * @version $Id: Ad.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/AdminUtil.class.php');

class Ad extends AdminUtil {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function a($controller, $request) {

		$to = newdb($this->dsn);

		$db = odbc_connect("tt", "sa", "sa", SQL_CUR_USE_ODBC);

		$sql = "select faddress, fphone, ffax, fshortname, fcontact, fnumber, fname from t_supplier ";
		$result = odbc_exec($db, $sql);
		while ($row = odbc_fetch_object($result)) {
			// print_r($row);

			$code = $row->fnumber;

			$tel = $row->fphone;
			$fax = $row->ffax;

			$name = $row->fshortname;
			$fullname = $row->fname;

			$address = $row->faddress;
			$contact = $row->fcontact;

			$fmt = "INSERT INTO %s(code, name, fullname, tel, fax, city, address, contact, createtime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now())";
			$sql = sprintf($fmt, Constant::tbl_supplier, $code, $name, $fullname, $tel, $fax, $address, $address, $contact);
			// debug($sql);

			$sql = iconv("GBK", "UTF-8", $sql);
			$to->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function b($controller, $request) {
		$db = newdb($this->dsn);

		$sql = "SELECT * FROM admin.country order by zone";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			// print_r($row);
			$fmt = "update admin.rate set code = '%s' where zone like '%s'";
			$sql = sprintf($fmt, $row['code'], $row['zone'] . "%");
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function d($controller, $request) {

		$string = " +972-3 -9023 597 ext ";
		if (preg_match('/^(\\+|-|\s|\d)+$/i', $string)) {
			$pattern = array('/\\+/', '/-/', '/\s/');
			$replacement = array('00', '', '');
			echo preg_replace($pattern, $replacement, $string);
		} else {
			echo "222";
		}



		logdate($controller, $request);
		return true;
	}

	function aa($country) {
		$db = newdb($this->dsn);
		$sql = "SELECT * FROM admin.rate where name like '" . $country . "%' order by rate";

		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row['rate'];
		}

		return '0.00';
	}

	function nationality($tbl, $name) {
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s WHERE name = '%s'";
		$sql = sprintf($fmt, $tbl, $name);
		// debug($sql);
		$result = $db->query($sql);

		if ($row = $db->fetch_array($result)) {
			return $row['code'];
		}

		return "-";
	}

	function h($controller, $request) {
		$aa = array(
			'Action'=>'OriginateCall', 
			'Source'=>'SIP/6595',
			'CallerID'=>'6595', 
			'Destination'=>'913760120595', 
			'Type'=>'default', 
		);

		$json = json_encode($aa);

		$this->call($json);

		logdate($controller, $request);
		return true;
	}

	function i($controller, $request) {
		$aa = array(
			'Action'=>'OriginateCall', 
			'Source'=>'SIP/6595',
			'CallerID'=>'6595', 
			'Destination'=>'913760120595', 
			'Type'=>'default', 
		);

		require_once APP_LIB . DIRECTORY_SEPARATOR . 'phpagi-asmanager.php';

		$cfg = array(
			'server'=>'192.168.2.245',
			'username'=>'admin',
			'secret'=>'amp111',
		);
		$am = new AGI_AsteriskManager(NULL, $cfg);
		$am->connect();

		$am->Originate('SIP/6595', '913760120595', 'tt', '1', NULL, NULL, 8000, '6595', NULL, NULL, NULL, NULL);

		$am->disconnect();
		logdate($controller, $request);
		return true;
	}

	function call($json) {
		$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if ($sock === false) {
			$error  = "!! Monast Error !!\n\nCould not create socket!\n";
			$error .= "reason: " . socket_strerror(socket_last_error());
			echo $json->encode(array(array('Action' => 'Error', 'Message' => $error)));
			die;
		}

		$conn = socket_connect($sock, "192.168.2.245", 5039);
		if ($conn === false) {
			$error  = "!! MonAst ERROR !!\n\nCould not connect to " . HOSTNAME . ":" . HOSTPORT . ": " . socket_strerror(socket_last_error()) . "\n";
			$error .= "Make sure monast.py is running so the panel can connect to its port properly.";
			echo $json->encode(array(array('Action' => 'Error', 'Message' => $error)));
			die;
		}

		socket_write($sock, "SESSION: " . time());
		socket_read($sock, 1024 * 16);
		socket_write($sock, $json . "\r\n");
		socket_write($sock, "BYE");

		socket_close($sock);
	}

	function k($controller, $request) {

		require_once(APP_LIB . '/SecureImageCode.class.php');

		$sc = new SecureImageCode(12, 11, '');
		$vcode = $sc->GenerateCode();

		echo $vcode;

		logdate($controller, $request);
		return true;
	}
	
	function n($controller, $request) {
		$fmt = "/sbin/iptables -A JR-NS-Firewall-1 -s 192.168.2.%s -j ACCEPT";
		$fmt = "/sbin/iptables -A JR-SJ-Firewall-1 -s 192.168.3.%s -j ACCEPT";
		
		for ($i = 32; $i < 70; $i++) {
			$s = sprintf($fmt, $i);
			
			echo $s . "\n";
		}

		logdate($controller, $request);
		return true;
	}
	
	function m($controller, $request) {
		$dir = "/home/sunxy/Videos";
		$this->mm($dir);
		
		$controller->assign_by_ref('result', $this->result);

		$controller->display($request, 'b');

		// logdate($controller, $request);
		return true;
	}
	
	
	function mm($dir) {
		$dh = opendir($dir);
		while (($file = readdir($dh)) !== false) {
			if ($file == '.' || $file == '..') {
				continue;
			}
			
			if (preg_match("/^_/i", $file)) {
				continue;
			}		
			
			if (is_dir($dir . '/' . $file)) {
				$this->mm($dir . '/' . $file);
			} else {
				if (preg_match("/mp4$/i", $file)) {
					$flv = str_replace("mp4", "flv", $file);
					$this->result[] = array('flv'=>$flv);
					
					$flv = '/home/sunxy/flv/' . $flv;
					
					$fmt = "/usr/local/ffmpeg/bin/ffmpeg -i %s -y -ab 32 -ar 22050 %s\n";
					// echo $dir . '/' . $file . "\n";
					$cmd = sprintf($fmt, $dir . '/' . $file, $flv);
					
					
					
					if (empty($flv) || file_exists($flv)) {
						continue;
					}
			
					echo $cmd;
					
					shell_exec($cmd);
				}
					
			}
		}
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function t($controller, $request) {
		$s = "0791-3732256";
		
		echo $this->call_number($s, '1');

		$controller->display($request, 't');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_barcode($controller, $request) {
		$s = '1234567890';

		require_once 'Image/Barcode.php';
		Image_Barcode::draw($s, 'Code39', 'png');

		return true;
	}

	function ip($controller, $request) {

		$a = 123456.78;
		
		$a = 123400.08;
		// $a = 12300.02;
		$a = 12345689.241;
		$b = $this->rmb($a);
		
		echo sprintf("%s\n%s", $a, $b);

		logdate($controller, $request);
		return true;
	}
		
	function rmb($s) {
		$ret = "零元整";
		if (eregi("[^0-9.]", $s)) {
			return $ret;
		}
		
		if ($s == 0 || $s == "") {			
			return $ret;
        }
        
        $digits = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
		$radices = array("", '拾', '佰', '仟',);
		$bigRadices = array("", '万', '亿');
		$decimals = array('角', '分', '厘', '毫');
		
		$arr = explode(".", $s); 	//按小数点切割金额
		
		// print_r($arr);
		
		$len1 = strlen($arr[0]); 	//整数位长度
		$len2 = isset($arr[1]) ? strlen($arr[1]) : 0; //小数位长度
		
		// echo sprintf("%s %s\n", $len1, $len2);
		
		$zeroCount = 0;
		
		$ret = "";
		// Process integral part if it is larger than 0:
		if (intval($arr[0]) > 0) {
			$zeroCount = 0;
			for ($i = 0; $i < $len1; $i++) {
				$p = $len1 - $i - 1;
				$d = substr($arr[0], $i, 1);
				// echo sprintf("%s %s\n", $p, $d);
				
				$quotient = $p / 4;
				$quotient = floor($p / 4);
				$modulus = $p % 4;
				
				// echo sprintf("quotient = %s, modules = %s\n", $quotient, $modulus);
				if ($d == "0") {
					$zeroCount++;
				} else {
					if ($zeroCount > 0) {
						$ret .= $digits[0];
					}
					$zeroCount = 0;
					$ret .= $digits[intval($d)] . $radices[$modulus];
				}
				
				if ($modulus == 0 && $zeroCount < 4) {
					$ret .= $bigRadices[$quotient];
				}
			}
			
			$ret .= "元";
		}
		
		// Process decimal part if there is:
		$decimal = $arr[1];
		if ($len2 > 0) {
			for ($i = 0; $i < $len2; $i++) {
				$d = substr($arr[1], $i, 1);
				if ($d != "0") {
					$ret .= $digits[intval($d)] . $decimals[$i];
				} else {
					$ret .= "零";
				}
			}
		} else {
			$ret .= "整";
		}
					
		return $ret;
	}

	function z($controller, $request) {
		$t = $this->bb("-1 hour 1 day");
		echo $t;

		logdate($controller, $request);
		return true;
	}
	
	function bb($s) {		
		$t = strtotime($s);
		
		return $t - time();		
	}
	
	function x($controller, $request) {
		$s = "111,test";
		echo $this->_input($s);

		logdate($controller, $request);
		return true;
	}
}
?>
